/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.HibernateException;

import org.hibernate.Session;
import systole.domain.persons.Patient;
import systole.domain.report.Report;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.ReportBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class ReportBrokerDB extends BrokerDB implements ReportBroker {

    /**
     *
     */
    public ReportBrokerDB() {
        super();
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.ReportBroker#getReportById(long)
     */
    @Override
    public Report getReportById(Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting report by id, id: " + id);
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Report instance = (Report) currentSession.get(Report.class, id);
            this.logger.logDebug("get successfully");
            return instance;
        } catch (HibernateException e) {
            this.logger.logError("error on get report, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener el Reporte", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.ReportBroker#getReportsByPatient(systole.domain.persons.Patient)
     */
    @Override
    public List<Report> getReportsByPatient(Patient patient) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting reports by patient");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Report> list = currentSession.createCriteria(Report.class).list();
            this.logger.logDebug("getting successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get reports by patient, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los reportes del paciente", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.ReportBroker#insert(systole.domain.report.Report)
     */
    @Override
    public void insert(Report report) throws ExceptionDAO {
        try {
            this.logger.logDebug("save report ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(report);
            this.logger.logDebug("save successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save report, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.ReportBroker#update(systole.domain.report.Report)
     */
    @Override
    public void update(Report report) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating report ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(report);
            this.logger.logDebug("update successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update report, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }
}
